/*
 * Copyright (c) XCI. All rights reserved.
 * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 */

package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.base.WebController;
import com.xci.core.domain.LockUserModel;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.helper.Helper;
import com.xci.core.web.JsonGrid;
import com.xci.sys.service.LockUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 锁定用户控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/sys/lockUser")
public class LockUserController extends WebController {
    /**
     * 系统锁定用户服务
     */
    @Resource
    private LockUserService lockUserService;

    //region 页面视图

    /**
     * 锁定用户首页
     */
    @GetMapping
    @Authorize(code = "sys.lockUser")
    public String index() {
        return "sys/lockuser/index";
    }

    /**
     * 锁定用户详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        LockUserModel entity = lockUserService.get(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        return "sys/lockuser/details";
    }

    //endregion

    //region 数据处理

    /**
     * 锁定用户表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "sys.lockUser.select")
    public JsonGrid grid(String name) {
        return RestMessage.grid(lockUserService.query(name));
    }

    /**
     * 移除锁定用户
     */
    @ResponseBody
    @PostMapping("/remove")
    @Authorize(code = "sys.lockUser.remove")
    public RestMessage remove(String accounts) {
        for (String account : Helper.splitToArray(accounts)) {
            lockUserService.remove(account);
        }
        return RestMessage.success();
    }

    //endregion
}
